package com.smartfarm.Guanchazhe;
//观察者模式相关
import com.smartfarm.Guanchazhe.Observer;
import com.smartfarm.Guanchazhe.Subject;

import java.util.ArrayList;
import java.util.List;

public class EnvironmentSensor implements Subject {
    private List<Observer> observers = new ArrayList<>();
    private String sensorData;

    public void setSensorData(String data) {
        this.sensorData = data;
        notifyObservers();
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        observers.remove(o);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(sensorData);
        }
    }
}